package com.study.base.netty.nettycase;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @description: NettyServet
 * @date: 2021/1/23 13:54
 * @author: hcm
 * @version: 1.0
 */
public class NettyServet {
    public static void main(String[] args) throws InterruptedException {
        //创建BossGroup和WorkGroup
        //boosGroup只是处理连接请求，真正和客户端处理，会交给workgrop完成
        //两个都是无限循环
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();
        //创建服务器的启动对象，配置参数
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        //使用链式编程经行设置
        //设置两个线程组
        serverBootstrap.group(bossGroup,workGroup)
                //使用NioSocketChannel作为通道实现
                .channel(NioServerSocketChannel.class)
                //设置线程队列链接个数
                .option(ChannelOption.SO_BACKLOG,128)
                //设置保持活动连接状态
                .childOption(ChannelOption.SO_KEEPALIVE,true)
                //管道测试
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    //给pipeline设置处理器
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(null);
                    }
                }); //管道处理器
        System.out.println("服务器准备好了");
        //绑定端口,并且同步处理，生成了一个channelfuture对象
        ChannelFuture cf = serverBootstrap.bind(666).sync();
        cf.channel().closeFuture().sync();

    }
}
